package com.tensquare.spit.controller;

import com.tensquare.common.auth.annotation.Authority;
import com.tensquare.common.auth.constant.AuthorityCode;
import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.tensquare.common.util.SecurityUtils;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-15 14:18
 **/
@RestController
@RequestMapping(value = "/spit")
public class SpitController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SpitService spitService;

    @Autowired
    private SecurityUtils securityUtils;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public Result findAll(){
        return new Result(true, StatusCode.OK, "查询成功", spitService.findAll());
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true, StatusCode.OK, "查询成功", spitService.findById(id));
    }

    /**
     * 新增
     * @param spit
     * @return
     */
    @Authority(AuthorityCode.USER)
    @PostMapping
    public Result add(@RequestBody Spit spit){
        spit.setUserid(securityUtils.getCuurentClaims().getId());
        spit.setNickname((String) securityUtils.getCuurentClaims().get("nickname"));
        spitService.add(spit);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 修改
     * @param spit
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result add(@RequestBody Spit spit, @PathVariable String id){
        spit.setId(id);
        spitService.update(spit);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result add(@PathVariable String id){
        spitService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 分页查询
     * @param parentid 上级id
     * @param page 页码数
     * @param size  分页条数
     * @return
     */
    @GetMapping(value = "/comment/{parentid}/{page}/{size}")
    public PageResult<Spit> findByParentid(@PathVariable String parentid, @PathVariable int page, @PathVariable int size){
        Page<Spit> pageData = spitService.findByParentid(parentid, page, size);
        return new PageResult<Spit>(pageData.getTotalElements(), pageData.getContent());
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    @Authority("user")
    @PutMapping(value = "/thumbup/{id}")
    public Result thumbup(@PathVariable String id){
        String userid = securityUtils.getCuurentUserid();
        if(redisTemplate.opsForValue().get("spit_thumbup_"  +  userid + "_" + id) != null){
            return new Result(false, StatusCode.ERROR, "你已经点过赞了");
        }
        spitService.updateThumbup(id);

        redisTemplate.opsForValue().set("spit_thumbup_" + userid + "_" + id, "1");
        return new Result(true, StatusCode.OK, "点赞成功");
    }

    /**
     * 追加浏览数
     * @param id
     * @return
     */
    @PutMapping(value = "/visits/{id}")
    public Result visits(@PathVariable String id){
        spitService.updateVisits(id);
        return new Result(true, StatusCode.OK, "浏览量追加成功");
    }

    /**
     * 追加分享数
     * @param id
     * @return
     */
    @PutMapping(value = "/share/{id}")
    public Result share(@PathVariable String id){
        spitService.updateShare(id);
        return new Result(true, StatusCode.OK, "分享数追加成功");
    }

}
